package com.ossjk.oa.finance.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.mapper.Condition;
import com.baomidou.mybatisplus.mapper.SqlHelper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.ossjk.core.util.CommonMethod;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.core.vo.PagePreNextVo;
import com.ossjk.oa.accounting.entity.CwCapitals;
import com.ossjk.oa.accounting.service.ICwCapitalsService;
import com.ossjk.oa.finance.entity.Reimburse;
import com.ossjk.oa.finance.entity.Subreimburse;
import com.ossjk.oa.finance.mapper.ReimburseMapper;
import com.ossjk.oa.finance.service.IReimburseService;
import com.ossjk.oa.finance.service.ISubreimburseService;
import com.ossjk.oa.finance.vo.ReimVo;
import com.ossjk.oa.system.entity.Employee;
import com.ossjk.oa.system.service.IEmployeeService;

/**
 * <p>
 * 报销 服务实现类
 * </p>
 *
 * @author chair
 * @since 2019-02-07
 */
@Service
public class ReimburseServiceImpl extends ServiceImpl<ReimburseMapper, Reimburse> implements IReimburseService {

	@Autowired
	private IReimburseService iReimburseService;
	@Autowired
	private ISubreimburseService iSubreimburseService;
	@Autowired
	private ICwCapitalsService iCwCapitalsService;
	@Autowired
	private IEmployeeService iEmployeeService;

	public PagePreNextVo selectPreOrNextByMth(String midMth) {

		PagePreNextVo vo = new PagePreNextVo();

		vo.setPre(this.baseMapper.selectPreMth(midMth));
		vo.setNext(this.baseMapper.selectNextMth(midMth));
		vo.setCurrent(midMth);
		// 取得6个月以前

		vo.setSelectAll(this.baseMapper.selectMax12Mth(CommonMethod.getxxMthBefore(midMth, -6)));

		return vo;
	}

	
	public PagePreNextVo selectPreNext(String midMth) {

		PagePreNextVo vo = new PagePreNextVo();
		String tmp =this.baseMapper.selectPreMth(midMth);
		vo.setPre(tmp==null?" ":tmp);
		tmp =this.baseMapper.selectNextMth(midMth);
		vo.setNext(tmp==null?" ":tmp );
		vo.setCurrent(midMth);
		 
		return vo;
	}
	
	@Override
	public boolean updateDetail(Reimburse reimburse, String[] sname, String[] samount, String[] sisreceipt,
			String[] sremarks) {
		// TODO Auto-generated method stub
		if (!CommonUtil.isBlank(sname) && !CommonUtil.isBlank(samount) && !CommonUtil.isBlank(sisreceipt)) {
			if (sname.length == samount.length && sname.length == sisreceipt.length) {
				Wrapper wrapper = Condition.create().like("rid", reimburse.getId());
				iSubreimburseService.delete(wrapper);
				List list = new ArrayList();
				for (int i = 0; i < sname.length; i++) {
					Subreimburse subreimburse = new Subreimburse();
					subreimburse.setName(sname[i]);
					subreimburse.setRid(reimburse.getId());
					subreimburse.setAmount(new BigDecimal(samount[i]));
					subreimburse.setIsreceipt(new BigDecimal(sisreceipt[i]));
					subreimburse.setRemarks(sremarks[i]);
					list.add(subreimburse);
				}
				iSubreimburseService.insertBatch(list);
			}
		}
		// 更新基础信息
		return this.updateById(reimburse);

	}

	public Reimburse getReimburse(Reimburse reimburse) {
		return this.baseMapper.selectOne(reimburse);
	}

	@Override
	public boolean insert(Reimburse reimburse, String[] sname, String[] samount, String[] sisreceipt,
			String[] sremarks) {
		if (!CommonUtil.isBlank(sname) && !CommonUtil.isBlank(samount) && !CommonUtil.isBlank(sisreceipt)) {

			if (CommonUtil.isBlank(reimburse.getEdtm())) {
				reimburse.setEdtm(reimburse.getSttm());
			}

			iReimburseService.insert(reimburse);
			if (sname.length == samount.length && sname.length == sisreceipt.length) {
				List list = new ArrayList();
				for (int i = 0; i < sname.length; i++) {
					Subreimburse subreimburse = new Subreimburse();
					subreimburse.setRid(reimburse.getId());

					subreimburse.setName(sname[i]);
					subreimburse.setAmount(new BigDecimal(samount[i]));
					subreimburse.setIsreceipt(new BigDecimal(sisreceipt[i]));
					subreimburse.setRemarks(sremarks[i]);

					list.add(subreimburse);
				}

				iSubreimburseService.insertBatch(list);
			}
		}
		return true;
	}

	@Override
	public Page selectVoPage(Page page, Wrapper wrapper) {

		// TODO Auto-generated method stub
		SqlHelper.fillWrapper(page, wrapper);
		page.setRecords(this.baseMapper.selectVosPage(page, wrapper));

		return page;
	}

	@Override
	public List<ReimVo> selectVo(int type, Wrapper wrapper) {

		if (type == 2) {
			return this.baseMapper.selectVos2(wrapper);
		} else if (type == 5) {
			return this.baseMapper.selectVos5(wrapper);
		}
		return this.baseMapper.selectVos(wrapper);
	}

	@Override
	public List<ReimVo> selectMthReimVos(Wrapper wrapper) {

		return this.baseMapper.selectMthVos(wrapper);
	}

	@Override //
	public boolean pay(Employee emp, Integer count, String mth, String descb, String cpttype) {
		
		Wrapper wrapper = Condition.create().eq("crer", emp.getId()).eq("rwst", 5).eq("left(edtm,7)", mth);

		List<Reimburse> rms = baseMapper.selectList(wrapper);
		BigDecimal sumV = BigDecimal.valueOf(0);
		System.out.println(rms.size() +"=="+ count);
		if (rms.size() == count) { // 校验数量
			for (Reimburse reimburse : rms) {
				reimburse.setRwst(BigDecimal.valueOf(7));
				baseMapper.updateById(reimburse);
				sumV = sumV.add(reimburse.getAmount());
			}
		} else {
			return false;
		}

		CwCapitals cw = new CwCapitals();
		cw.setCptdate(new Date());
		cw.setCptacount(sumV);
		cw.setCptname(emp.getRname() + mth + "月报销");
		cw.setFlag(0);
		cw.setTypec("报销");
		cw.setDescb(descb);
		cw.setCpttype(cpttype);

		return iCwCapitalsService.insert(cw);
	}

	@Override
	public List<ReimVo> selectMthVoCount(Wrapper wrapper) {
		// TODO Auto-generated method stub
		return this.baseMapper.selectMthVoCount(wrapper);
	}

	@Override
	public boolean pays(String[] eids, String mth,   String cpttype) {
		System.out.println(mth);
		Wrapper wrapper = null;
		CwCapitals cw = null;
		Employee emp = null;
		List<Reimburse> rms = null;

		for (String eid : eids) {
			wrapper = Condition.create().eq("crer", eid).ge("rwst", 5).eq("left(edtm,7)", mth);
			emp = iEmployeeService.selectById(eid);
			rms = baseMapper.selectList(wrapper);
			BigDecimal sumV = BigDecimal.valueOf(0);

			for (Reimburse reimburse : rms) {
				reimburse.setRwst(BigDecimal.valueOf(7));
				baseMapper.updateById(reimburse);
				sumV = sumV.add(reimburse.getAmount());
			}

			cw = new CwCapitals();
			cw.setCptdate(new Date());
			cw.setCptacount(sumV);
			cw.setCptname(emp.getRname() + mth + "月报销");
			cw.setFlag(0);
			cw.setTypec("报销");
			cw.setDescb("批量支付");
			cw.setCpttype(cpttype);

			iCwCapitalsService.insert(cw);

		}

		return true;
	}

}
